package com.ruoyi.web.controller.system;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.constant.WxMaConstants;
import com.ruoyi.common.core.domain.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * @desc: 微信小程序订阅消息推送接口
 * @author: cao_wencao
 * @date: 2020-08-06 14:22
 */
@Slf4j
@RestController
@RequestMapping("/api/msg")
public class WxMsgPushController {


    @Autowired
    private WxMaService wxService;


    /**
     * 微信小程序推送订阅消息
     *
     * @param openid
     * @return
     */
    @RequestMapping("/push")
    public AjaxResult push(String openid) {
        log.info("openid:"+openid);
        WxMaSubscribeMessage message = new WxMaSubscribeMessage();
        message.setTemplateId("SANOZnhIgeTvole_wXYMIVA3Koy9UR0Cdh0bAJo-7so");
        message.setToUser(openid);
        message.setLang(WxMaConstants.MiniProgramLang.ZH_CN);
        message.setMiniprogramState(WxMaConstants.MiniProgramState.FORMAL);
        message.addData(new WxMaSubscribeMessage.MsgData("thing1", "病人互助"));
        message.addData(new WxMaSubscribeMessage.MsgData("thing4", "西藏左贡县旺达中街43号"));
        try {
            this.wxService.getMsgService().sendSubscribeMsg(message);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success("推送成功");
    }
}